using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.DataService;
using Prodma.DataAccess.DataService.ListService;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.Authorization;
using Prodma.DataAccess.Repositories;

namespace Prodma.Panel.Controllers
{
    [Authorize]
    public class ProjectController : Controller
    {
        
        #region Repository Setters

        private IProjectRepository repository;

        public ProjectController() : this(new ProjectService()) { }

        public ProjectController(IProjectRepository _repository)
        {
            repository = _repository;
        }

        #endregion


        #region Index
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Read)]
        public ActionResult Index()
        {
            if (Request.QueryString["find"] != "" && Request.QueryString["find"] != null)
            {
                Dictionary<string, string> find = new Dictionary<string, string>();
                find.Add("name", Request.QueryString["find"]);
                return View(repository.GetList(find));
            }
            else
            {
                return View(repository.GetList());
            }
        }
        #endregion


        #region New
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Write)]
        public ActionResult New()
        {
            ViewData["stateList"] = StateList.GetList(StateType.General);

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Write)]
        public ActionResult New(FormCollection form)
        {
            ViewData["stateList"] = StateList.GetList(StateType.General);

            tb_Project project = new tb_Project();
            project.Name = Request.Form["name"];
            project.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(Request.Form["ddlState"]));

            repository.New(project);
            return View();
        }
        #endregion


        #region Edit
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Update)]
        public ActionResult Edit()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            ProjectVm project = repository.Edit(id);
            
            ViewData["stateList"] = StateList.GetList(StateType.General, project.State.Id);

            return View(project);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Update)]
        public ActionResult Edit(FormCollection form)
        {
            tb_Project project = new tb_Project();
            project.Name = Request.Form["name"];
            project.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(Request.Form["ddlState"]));

            int id = Convert.ToInt32(Request.Form["id"]);
            repository.Update(id, project);

            return RedirectToAction("edit/?id=" + id);
        }
        #endregion


        #region Delete
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Delete)]
        public ActionResult Delete()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string deleteId = id.Substring(3, id.Length - 3);
                repository.Delete(Convert.ToInt32(deleteId));
            }

            return RedirectToAction("index");
        }
        #endregion


        #region Set Active && Passive
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Update)]
        public ActionResult SetActive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                repository.SetActive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        [NavigationFilter(MenuAdmins.ProjectManagement, AuthorizationType.Update)]
        public ActionResult SetPassive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                repository.SetPassive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        #endregion

    }
}
